<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	$tomid=intval($_RQ['tomid']);
	$tcpid=intval($_RQ['tcpid']);
	$item=$this->model('kefu')->gettcp($tcpid,$tomid);
	if(!$item['id']){
		exi('当前对话不存在','error','/index/');
	}
	if($item['mid']!=$_SESSION['uid']){
		$item['tomid']=$item['mid'];
		$item['mid']=$_SESSION['uid'];
	}
	if($item['tomid']==$item['mid']){
		exi('不能跟自己对话','error','/index/');
	}
	$this->session('tcpid',$item['id']);
	$member=$this->model('member')->get_member($item['mid']);
	if(!$member['avatar']){
		$symember=model('core/member')->detail($item['mid']);
		$member['avatar']=$symember['avatar'];
	}
	$worker=$this->model('member')->get_member($item['tomid']);
	if(!$worker['avatar']){
		$symember=model('core/member')->detail($item['tomid']);
		$worker['avatar']=$symember['avatar'];
	}
	$member['avatar']=tomedia($member['avatar']);
	$worker['avatar']=tomedia($worker['avatar']);
	$worker['images']=json_decode($worker['images'],true);
	if(count($worker['images'])<4){
		$worker['images2']=array();
		$num=4-count($worker['images']);
		for($i=0;$i<$num;$i++){
			$worker['images2'][]=$i;
		}
	}
	$worker['qmdu']=pdo_count('jy_ailiao_tcplog',array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$item['id']));
	$worker['qmdu']=$worker['qmdu']/10;
	$worker['lat_lng']=json_decode($worker['lat_lng'],true);
	if($worker['lat_lng']['lng'] && $worker['lat_lng']['lat']){
		$lat_lng=$this->session('lat_lng');
		$worker['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$worker['lat_lng']['lng'],$worker['lat_lng']['lat']);
		$worker['juli']=$worker['juli']?price($worker['juli']/1000,'int'):0;
	}else{
		$worker['juli']=0;
	}
	$item['worker']=$worker;
	$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
	$item['statustitle']=$this->tcp_status[$item['status']];
	$return['openid']= $_SESSION['openid'];
	$return['member']= $member;
	$return['item']= $item;
	$return['list']= array();
	$return['post']= array('msg'=>'','tcpid'=>$item['id'],'workerid'=>$item['workerid'],'star'=>5);
	$return['params']= array('tcpid'=>$item['id']);
	$liping=pdo_getall('jy_ailiao_liping',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['liping']=$liping;
	if($this->plugin_exists('jy_videocall')){
		$return['videocallurl']=custom_url('jy_videocall/index//pluginname=jy_ailiao&tomid='.$item['tomid'].'&videobi='.$return['pluginset']['liao_videobi'],true);
		if($_RQ['endcall']){
			$this->model('kefu')->videocall($item['tomid'],1);
		}
	}
	set_html('title',$worker['username']?:$worker['nickname']);
	exi($return);
}
if($_RQ['op']=='list'){
	$tcpid=$_RQ['tcpid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total = 0;
	$list=pdo_getslice('jy_ailiao_tcplog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$chatlogtime = time();
	if($list){
		foreach($list as $k=>&$v){
			$settime = $v['createtime'];
			if(($chatlogtime - $v['createtime']) > 600 || $k+1==count($list)){
				$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
			}else{
				$v['createtime'] = '';
			}
			$chatlogtime = $settime;
			$v['msg'] = cfc('tools')->filterword($v['msg'],$setting['filterword']);
			$v['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $v['msg']);
			$hrefhtml = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
			preg_match_all($hrefhtml,$v['msg'],$resarray);  
			if($v['type'] == 1 || $v['type'] == 2){
						foreach($resarray[0] as &$vv){
							if(!empty($vv)){
								$v['msg'] = str_replace($vv,"<a href='".$vv."'>".$vv."</a>",$v['msg']);
							}
						}
			}
			if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
				$v['msg'] = str_replace("\n","</br>",$v['msg']);
			}
			$v['fileinfo']=json_decode($v['fileinfo'],true);
			$v['goods']=json_decode($v['goods'],true);
			if($v['tomid']==$_SESSION['uid']){
				pdo_update('jy_ailiao_tcplog',array('isread'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['id']));
			}
			$v['audioleng'] = $v['audiotime']>2?$v['audiotime']:2;
		}
	}
	$return['list']= $list;
	exi($return);
}
if($_RQ['op']=='gethtml'){
	$id=intval($_RQ['id']);
	$chatid=intval($_RQ['chatid']);
	$getmemberlogo=$this->session('getmemberlogo');
	$memberlogo=$this->session('memberlogo');
	$info=pdo_get('jy_ailiao_tcplog',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$chatid,'id'=>$id));
	if($_SESSION['os']=='windows'){
		$htmltime = '<div class="message_box_time">'.$info['createtime'].'</div>';
		if($info['type'] == 1 || true){
			$html .= $htmltime;
			if($info['setmid'] != $_SESSION['uid']){
					$html .='<div class="message_box"><img class="mssage_box_pic" src="'.$getmemberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['answer'].'</p></div></div></div>';
			}else{
					$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['askmsg'].'</p></div></div></div>';
			}			
		}
	}else{
		if($info['type'] == 1 || true){
			if($info['mid'] == $_SESSION['uid']){
				$html .='<div class="r clearfix"><img class="pic"/><p class="text">'.$info['askmsg'].'</p></div>';
			}else{
				$html .='<div class="l clearfix"><img class="pic"/><p class="text">'.$info['answer'].'</p></div>';
			}
			$html .= $htmltime;
		}
	}
	$return['html']= $html;
	exi($return);
}
if($_RQ['op']=='addlog'){
	$postdata=get_postdata();
	$postdata['msg']=$postdata['msg2']?:$postdata['msg'];
	$res=$this->model('kefu')->addchatlog($postdata);
	exi($res);
}
if($_RQ['op']=='getmsg'){
	$tomid = trim($_RQ['tomid']);
	$tcpid = trim($_RQ['tcpid']);
	$chatmsg = trim($_RQ['msg2'])?:trim($_RQ['msg']);
	$getmsg=$this->model('model')->getmsg($chatmsg,'',$tomid);
	if($getmsg && $getmsg!='fail'){
		$postdata=array(
			'tcpid'=>$tcpid,
			'msg'=>$getmsg,
			'type'=>0,
			'tomid'=>$tomid,
			'is_worker'=>1,
		);
		$res=$this->model('kefu')->addchatlog($postdata);
	}else{
		$res=array('msg'=>'无');
	}
	exi($res);
}
if($_RQ['op']=='getnewlog'){
	$tcpid = trim($_RQ['tcpid']);
	$is_worker = intval($_RQ['is_worker']);
	$lastid = intval($_RQ['lastid']);
	$log = pdo_get('jy_ailiao_tcplog',array('id >'=>$lastid,'uniacid'=>$_SESSION['uniacid'],'tomid'=>$_SESSION['uid'],'tcpid'=>$tcpid));
	if($log){
			pdo_update('jy_ailiao_tcplog',array('isread'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['id']));
			$chatlogtime = time();
			$settime = $log['createtime'];
			$log['createtime'] = '';
			$log['msg'] = cfc('tools')->filterword($log['msg'],$setting['filterword']);
			$log['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $log['msg']);
			$hrefhtml = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
			preg_match_all($hrefhtml,$log['msg'],$resarray);  
			if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
				$log['msg'] = str_replace("\n","</br>",$log['msg']);
			}
			$log['fileinfo']=json_decode($log['fileinfo'],true);
			$log['goods']=json_decode($log['goods'],true);
			$res['log']=$log;
	}else{
		$res=array('log'=>false);
		$this->model('kefu')->sendmsg($tcpid,$lastid);
	}
	exi($res);
}
if($_RQ['op']=='sendlp'){
	$id=$_RQ['tcpid'];
	if(!$_RQ['tcpid']){
		$tcpid=$this->session('tcpid');
	}
	if(!$tcpid){
		exi('未正确获取对话信息','error');
	}
	$postdata=get_postdata();
	$lpid=$postdata['lpid'];
	if(!$lpid){
		exi('未获取到礼品信息','error');
	}
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname'));
	$tcp=pdo_get('jy_ailiao_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tcpid));
	$tomid=$tcp['tomid']==$_SESSION['uid']?$tcp['mid']:$tcp['tomid'];
	$liping=pdo_get('jy_ailiao_liping',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lpid));
	if($member['credit3']<$liping['price']){
		exi('当前金币数不足！无法送礼！','error');
	}
	//礼品订单
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tomid'=>$tomid,
			'tcpid'=>$tcpid,
			'lpid'=>$lpid,
			'fee'=>$liping['price'],
			'ordersn'=>get_ordersn('jy_ailiao_sendlplog','ordersn'),
			'jifen'=>$liping['jifen'],
			'status'=>1,
			'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_ailiao_sendlplog',$insert);
	if($liping['jifen']){
		pdo_update('jy_ailiao_member',array('zannum +='=>$liping['jifen']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$tomid));
	}
	$postdata=array(
			'tcpid'=>$tcpid,
			'goodsid'=>$lpid,
			'type'=>8,
			'tomid'=>$tomid,
	);
	$res=$this->model('kefu')->addchatlog($postdata);
	exi($res);
}
if($_RQ['op']=='check_buy'){
	$postdata=get_postdata();
	$tcpid = $postdata['tcpid'];
	if(!$tcpid){
		exi('请选择对话！','error');
	}
	$item=pdo_get('jy_ailiao_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tcpid));
	$price=$this->setting['liao_bi'];
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','mobile','avatar','nickname'));
	if($member['credit3']<$price){
		exi('当前金币不足，请先充值！','error','jy_ailiao/member.index/');
	}
	model('core/member')->member_credit($_SESSION['uid'],'credit3',$price,false,'jy_ailiao','男生聊天按句收费');
	$this->model('commission')->upliaofee($item,$price);
	exi($return);
}
if($_RQ['op']=='playaudio'){
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	$update=array('isread'=>1);
	pdo_update('jy_ailiao_tcplog',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi($return);
}